// pages/posts/post-detail/post-detail.js
import {
  getTaTravelDetailPage,
  getCmCommentPage,
  getCmCommentAppendPage,
  insertUaCollect,
  deleteUaCollectByIdList,
  getUaCollectPage
} from "../../../api/post.js";
import {
  insertCmComment
} from "../../../api/user.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    post: '',
    datalist: [{
      datalist1: [],
      total: 0
    }],
    datalist1: [],
    total: '',
    num: '', //评论数量
    inputValue: '',
    postid: 0,
    commentid: '',
    userid: 1,
    currentcolor: 0,
    collectid:0
  },
  changecolor(e) {
    let currentcolor = this.data.currentcolor;
    if (currentcolor == 0) {
      let data = {
        "uaUserId": 1,
        "collectType": 6,
        "collectTypeId": this.data.postid
      }
      this.insertUaCollect(data);
      this.setData({
        currentcolor: 1
      });
    }
    if (currentcolor == 1) {
      // let id = e.currentTarget.dataset.id;
      // id = JSON.stringify(id)
      let data = {
        idList: [],
      }
      data.idList.push(this.data.collectid)
      deleteUaCollectByIdList(data).then(res => {});
      this.setData({
        currentcolor: 0
      });
    }
  },
  insertUaCollect(data) {
    insertUaCollect(data).then(res => {
      console.log('插入成功')
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let id = JSON.parse(options.id);
    this.data.postid = id;
    this.getUaCollectPage(id)
    this.getTaTravelDetailPage(id)
    this.getCmCommentPage(id)

  },
  getUaCollectPage(id) {
    let data = {
      "uaUserId": 1,
      "collectType": 6,
      "collectTypeId": id,
    }
    getUaCollectPage(data).then(res => {
      let datalist = res.data.data.records;
      console.log(datalist.length)
      let collectid=datalist[0].id
      this.data.collectid=collectid
      if (datalist.length === 0)
        this.data.currentcolor = 0;
      else {
        this.data.currentcolor = 1;
        console.log(this.data.currentcolor)
        this.setData({
          currentcolor: 1,
          collectid:collectid,
        });
      }

    })
  },
  getCmCommentPage(id) {
    let data = {
      "pageNum": "1",
      "pageSize": "10",
      "id": null,
      "uaUserId": null,
      "commentType": 6,
      "commentTypeId": id,
      "commentText": null,
      "praiseNum": null,
      "followNum": null,
      "status": null,
      "delFlag": null,
      "createBy": null,
      "createTime": null,
      "updateBy": null,
      "updateTime": null,
      "remark": null
    }
    getCmCommentPage(data).then(res => {
      let datalist = res.data.data.records;
      this.setData({
        datalist: datalist,
        num: datalist.length,

      })
      datalist.forEach((item, index) => {
        this.getCmCommentAppendPage(item.id, index)
      })
    })

  },
  getCmCommentAppendPage(id, index) {
    let data = {
      "pageNum": "1",
      "pageSize": "10",
      "id": null,
      "cmCommentId": id,
      "uaUserId": null,
      "commentText": null,
      "praiseNum": null,
      "status": null,
      "delFlag": null,
      "createBy": null,
      "createTime": null,
      "updateBy": null,
      "updateTime": null,
      "remark": null
    }
    getCmCommentAppendPage(data).then(res => {
      let datalist1 = res.data.data.records;
      console.log(datalist1)
      let total = res.data.data.total

      let datalist = this.data.datalist

      datalist[index].datalist1 = datalist1
      datalist[index].total = total

      this.setData({
        datalist: datalist,
      })
    })
  },
  getTaTravelDetailPage(id) {
    let data = {
      "pageNum": "1",
      "pageSize": "10",
      "id": null,
      "taTravelId": id,
      "detailText": null,
      "status": null,
      "delFlag": null,
      "createBy": null,
      "createTime": null,
      "updateBy": null,
      "updateTime": null,
      "remark": null
    }
    getTaTravelDetailPage(data).then(res => {
      let datalist = res.data.data.records[0];
      // console.log(datalist)
      this.setData({
        post: datalist
      })
    })
  },

  inputOver(e) {
    let comment = e.detail.value
    this.setData({
      inputValue: comment
    })
  },
  //点击发送按钮
  post() {
    let message = this.data.inputValue;
    console.log(message)
    let data = {
      "commentTypeId": this.data.postid,
      "commentType": 6,
      "commentText": message,
      "uaUserId": 1
    }
    this.insertCmComment(data);
    this.setData({
      inputValue: ""
    })
  },
  //添加评论
  insertCmComment(message) {

    // this.setData({
    //   datalist:data
    // })
    insertCmComment(message).then(res => {
      this.getCmCommentPage(this.data.postid)
    })

  },
  InputBlur(e) {
    this.setData({
      InputBottom: 0
    })
  },

})